define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'taskfans/index',
                    // add_url: 'vipuser/add',
                    // edit_url: 'anchor/edit',
                    // del_url: 'anchor/del',
                    // multi_url: 'anchor/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'livestreamerUser',
                sortName: 'taskBeginTime',
                templateView: false,
                clickToSelect: false,
                search: false,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'livestreamerUser', title: __('Host ID'), operate: 'like'},
                        {field: 'zone_list.zonename', title: __('District'), searchList: Config.zoneList},
                        {
                            field: 'taskBeginTime',
                            title: __('taskBeginTime'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: false
                        },
                        {
                            field: 'taskBeginTime',
                            title: __('Date'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: 'RANGE',
                            visible: false
                        },
                        {
                            field: 'taskEndTime',
                            title: __('taskEndTime'),
                            formatter: Table.api.formatter.datetime,
                            addclass: 'datetimerange',
                            operate: false
                        },
                        {field: 'currentFansNum', title: __('Initial Fans'), operate: false},
                        {field: 'taskNum', title: __('No. of Adding Fans Requst'), operate: false},
                        {field: 'completeNum', title: __('The Times of Successfully Adding Fans'), operate: false}
                    ]
                ]
            });

            $(document).on("click", ".btn-addjob", function () {
                var ids = Table.api.selectedids(table);
                var url = 'taskfans/addjob';
                if (url.indexOf("{ids}") !== -1) {
                    url = Table.api.replaceurl(url, {ids: ids.length > 0 ? ids.join(",") : 0}, table);
                }
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('Add'), $(this).data() || {});
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        addjob: function () {
            $(".btn-searchUser").on("click", function () {
                let id = $("#c-livestreamerUser").val();
                if (id == '' || id == null) {
                    Backend.api.toastr.error('请输入主播id');
                    return
                }
                Fast.api.ajax({
                    url: 'taskfans/searchUser?id=' + id,
                    loading: true
                }, function (data) {
                    $("#userNickName").text("会员等级：" + data.grade)
                    $("#ceil").text("剩余配额：" + data.fansSurplusNum)
                    $("#currentFans").val(data.fansNum)
                    $("#currentFansText").text("当前粉丝数： "+ data.fansNum)
                }, function (data, ret) {
                    Layer.msg(ret.msg)
                    return false
                })
            })

            Controller.api.bindevent()
        }
    };
    return Controller;
});
